<script setup>
import { useRoute, useRouter } from "vue-router";
import { ref, reactive, onMounted } from "vue";
import myAxios from "../plugins/myAxios.js";
import { Toast } from "vant";
const route = useRoute();
const router = useRouter();
// 解构赋值
let { editKey, editName, currentValue, userId } = route.query;
currentValue = ref(currentValue);
const onSubmit = () => {
    // 值判空
    if (currentValue.value == null || currentValue.value == "") {
        Toast.fail("修改值不能为空哦！");
        return;
    }
    if (editKey && editKey == "gender") {
        if(currentValue.value != '男' && currentValue.value != '女') {
            Toast.fail("性别只能为(男，女)哦！");
            return;
        } else {
            currentValue.value = currentValue.value == "男" ? 1 : 0;
        }
    }
    myAxios
        .put("/user", {
            id: userId,
            [editKey]: currentValue.value,
        })
        .then((res) => {
            if (res.data.code == 1) {
                Toast.success("修改成功");
                console.log("修改成功");
                // 返回
                router.back();
            } else {
                Toast.fail("修改失败");
                console.log("修改失败：" + res.data.msg);
            }
        });
};
</script>
<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                v-model="currentValue"
                :name="editKey"
                :label="editName"
                placeholder="请输入编辑信息"
            />
        </van-cell-group>
        <div style="margin: 16px">
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </div>
    </van-form>
</template>
<style scoped lang="scss"></style>
